using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using Customs.Components.KDT.ThanhKhoan.Info;
namespace Customs.Components.KDT.ThanhKhoan
{
    public class BKNPLNopThue : BKNPLNopThueInfo
    {
        public void Load(BKNPLNopThueInfo entity)
        {
            this._LUONG_NT = entity.LUONG_NT;
            this._MA_DVT = entity.MA_DVT;
            this._Ma_HQ = entity.MaHaiQuanTK;
            this._Ma_HQTN = entity.MaHaiQuan;
            this._Ma_LH = entity.MaloaiHinh;
            this._Ma_NPL = entity.MaNPL;
            this._Nam_TN = entity.NamTiepNhan;
            this._NamDK = entity.NamDangKy;
            this._NGAYDK = entity.NGAYDK;
            this._NGAYTH = entity.NGAYTH;
            this._So_TNDKDT = entity.SoTiepNhan;
            this._SoTK = entity.SoToKhai;
            this._STTHang = entity.STTHang;
            this._Ten_DVT = entity.Ten_DVT;
            this._TEN_NPL = entity.TenNPL;
        }
        public int Insert(SqlTransaction transaction)
        {
            StringBuilder query = new StringBuilder();
            query.Append("INSERT INTO DTLNPLNT_D (");
            query.Append("So_TNDKDT, ");
            query.Append("Ma_HQTN, ");
            query.Append("Nam_TN, ");
            query.Append("SOTK, ");
            query.Append("MA_LH, ");
            query.Append("NamDK, ");
            query.Append("MA_HQ, ");
            query.Append("MA_NPL, ");
            query.Append("TEN_NPL, ");
            query.Append("NGAYDK, ");
            query.Append("NGAYTH,  ");

            query.Append("LUONG_NT, ");
            query.Append("MA_DVT, ");
            query.Append("Ten_DVT, ");
            query.Append("STTHang ) ");

            query.Append("VALUES ( ");

            query.Append("@So_TNDKDT, ");
            query.Append("@Ma_HQTN, ");
            query.Append("@Nam_TN, ");
            query.Append("@SOTK, ");
            query.Append("@MA_LH, ");
            query.Append("@NamDK, ");
            query.Append("@MA_HQ, ");
            query.Append("@MA_NPL, ");
            query.Append("@TEN_NPL, ");
            query.Append("@NGAYDK, ");
            query.Append("@NGAYTH,  ");

            query.Append("@LUONG_NT, ");
            query.Append("@MA_DVT, ");
            query.Append("@Ten_DVT, ");
            query.Append("@STTHang ) ");

            SqlParameter[] parameters = new SqlParameter[15];

            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this.SoTiepNhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = this.MaHaiQuan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this.NamTiepNhan;

            parameters[3] = new SqlParameter("@SOTK", SqlDbType.BigInt);
            parameters[3].Value = this.SoToKhai;

            parameters[4] = new SqlParameter("@MA_LH", SqlDbType.Char, 5);
            parameters[4].Value = this.MaloaiHinh;

            parameters[5] = new SqlParameter("@NamDK", SqlDbType.SmallInt);
            parameters[5].Value = this.NamDangKy;

            parameters[6] = new SqlParameter("@MA_HQ", SqlDbType.VarChar, 14);
            parameters[6].Value = this.MaHaiQuanTK;

            parameters[7] = new SqlParameter("@MA_NPL", SqlDbType.VarChar, 30);
            parameters[7].Value = this.MaNPL;

            parameters[8] = new SqlParameter("@TEN_NPL", SqlDbType.NVarChar, 80);
            parameters[8].Value = this.TenNPL;

            parameters[9] = new SqlParameter("@NGAYDK", SqlDbType.DateTime);
            parameters[9].Value = this.NGAYDK;

            parameters[10] = new SqlParameter("@NGAYTH", SqlDbType.DateTime);
            parameters[10].Value = null;

            parameters[11] = new SqlParameter("@LUONG_NT", SqlDbType.Decimal);
            parameters[11].Value = this.LUONG_NT;

            parameters[12] = new SqlParameter("@MA_DVT", SqlDbType.Char, 3);
            parameters[12].Value = this.MA_DVT;

            parameters[13] = new SqlParameter("@Ten_DVT", SqlDbType.NVarChar);
            parameters[13].Value = this.Ten_DVT;

            parameters[14] = new SqlParameter("@STTHang", SqlDbType.Int);
            parameters[14].Value = this.STTHang;

            return SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query.ToString(), parameters);

        }

        public static int DeleteByForeignKey(SqlTransaction transaction, string mahq, long sotiepnhan, short namtiepnhan)
        {
            string sql = "delete from DTLNPLNT_D where So_TNDKDT=@So_TNDKDT and Ma_HQTN=@Ma_HQTN and Nam_TN=@Nam_TN";
            SqlParameter[] parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = sotiepnhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = mahq;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = namtiepnhan;
            return SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, sql, parameters);

        }

        public static BKNPLNopThueInfoCollection LoadCollection(string mahq, long sotiepnhan, short namtiepnhan)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
            string sql = "select * from DTLNPLNT_D where So_TNDKDT=@So_TNDKDT and Ma_HQTN=@Ma_HQTN and Nam_TN=@Nam_TN";

            SqlParameter[] parameters = new SqlParameter[3];
            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = sotiepnhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = mahq;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = namtiepnhan;


            SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, CommandType.Text, sql, parameters);
            BKNPLNopThueInfoCollection collection = new BKNPLNopThueInfoCollection();
            while (reader.Read())
            {
                BKNPLNopThueInfo info = new BKNPLNopThueInfo();
                if (!reader.IsDBNull(reader.GetOrdinal("So_TNDKDT"))) info.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("So_TNDKDT"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ma_HQTN"))) info.MaHaiQuan = reader.GetString(reader.GetOrdinal("Ma_HQTN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Nam_TN"))) info.NamTiepNhan = reader.GetInt16(reader.GetOrdinal("Nam_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("SOTK"))) info.SoToKhai = reader.GetInt32(reader.GetOrdinal("SOTK"));
                if (!reader.IsDBNull(reader.GetOrdinal("MA_LH"))) info.MaloaiHinh = reader.GetString(reader.GetOrdinal("MA_LH"));
                if (!reader.IsDBNull(reader.GetOrdinal("NAMDK"))) info.NamDangKy = reader.GetInt16(reader.GetOrdinal("NAMDK"));
                if (!reader.IsDBNull(reader.GetOrdinal("MA_HQ"))) info.MaHaiQuanTK = reader.GetString(reader.GetOrdinal("MA_HQ"));
                if (!reader.IsDBNull(reader.GetOrdinal("MA_NPL"))) info.MaNPL = reader.GetString(reader.GetOrdinal("MA_NPL"));
                if (!reader.IsDBNull(reader.GetOrdinal("TEN_NPL"))) info.TenNPL = reader.GetString(reader.GetOrdinal("TEN_NPL"));
                if (!reader.IsDBNull(reader.GetOrdinal("NGAYDK"))) info.NGAYDK = reader.GetDateTime(reader.GetOrdinal("NGAYDK"));
                if (!reader.IsDBNull(reader.GetOrdinal("NGAYTH"))) info.NGAYTH = reader.GetDateTime(reader.GetOrdinal("NGAYTH"));
                if (!reader.IsDBNull(reader.GetOrdinal("LUONG_NT"))) info.LUONG_NT = reader.GetDecimal(reader.GetOrdinal("LUONG_NT"));
                if (!reader.IsDBNull(reader.GetOrdinal("MA_DVT"))) info.MA_DVT = reader.GetString(reader.GetOrdinal("MA_DVT"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ten_DVT"))) info.Ten_DVT = reader.GetString(reader.GetOrdinal("Ten_DVT"));
                if (!reader.IsDBNull(reader.GetOrdinal("STTHang"))) info.STTHang = reader.GetInt32(reader.GetOrdinal("STTHang"));
                collection.Add(info);
            }
            reader.Close();
            return collection;

        }

    }
}
